package com.scalar.chiptrack.finance.reports.actions;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;

import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.finance.reports.forms.AccruedLiabilityReportForm;


/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: Dec 28, 2007
 * Time: 4:48:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class AccruedLiabilityReportAction extends ChipTrackAction
{
    private static final String ACTION_NAME = "/accruedLiabilityReportAction.do";
    
    public ActionForward execute ( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response ) throws Exception
    {
        String action = request.getParameter( "action" );

        HttpSession session = request.getSession();
        User user = ( User ) session.getAttribute( SessionKeys.USER_KEY );

        if( user == null )
        {
            session.setAttribute( SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME + "?" + request.getQueryString() );
            return ( mapping.findForward( "login" ) );
        }

        AccruedLiabilityReportForm accruedLiabilityReportForm   = ( AccruedLiabilityReportForm ) form;

        try
        {
            ArrayList processList   = new ArrayList();
            if ( action.equals( "accruedLiabilityReportAction" ) )
            {
               try
               {
                    ArrayList partNumberList    = null;
                    ArrayList vendorList        = null;

                    String psType               = null;
                    String psTypeName           = null;
                    String processType          = null;

                    processType  =  accruedLiabilityReportForm.getProcessType();
                    if ( !"0".equalsIgnoreCase( processType ) && processType.length() > 1 )
                    {
                        psType = processType.substring(0, processType.indexOf("/") );
                        psTypeName = processType.substring(processType.indexOf("/")+1, processType.length() );
                    }

                    processList = NewProductDAO.loadProcessList(null,"Wip");

                    if( psType == null || "0".equalsIgnoreCase ( psType ) )
                    {
                       partNumberList = NewProductDAO.loadAllBaseProducts( null, true, true );
                       psTypeName = "All";
                    }
                    else
                    {
                       partNumberList = NewProductDAO.loadPartNumbers( null, 0, psType, true, true, true, 0 );
                    }

                    vendorList = VendorDAO.getVendorNamesByProcessByUserType( null, psTypeName, user );

                    request.setAttribute( "processList", processList );
                    request.setAttribute( "partNumberList", partNumberList );
                    request.setAttribute( "vendorList" , vendorList );
                    request.setAttribute( "categoryList", NewProductDAO.loadCategoryList() );

                }
                catch( Exception e )
                {
                   e.printStackTrace();
                }
            }

        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        if( action.equals( "accruedLiabilityReportAction" ) )
        {
            return( mapping.findForward( "accruedLiabilityReportAction" ) ) ;
        }
        else
        return null;
    }

}
